import Vue from 'vue'
import store from '@/store/index'
let _this = Vue.prototype;

const utlis = {
	baseUrl: 'https://chuntai.shanghaijiudao.com/api',
	// baseUrl: 'https://api.youyuekeji.cc/api',
	Toast: (title, flag) => {
		uni.showToast({
			title,
			icon: flag ? 'success' : 'none',
		})
	},
	getNowDate(date,isDay) {
		var y = date.getFullYear();
		var m = date.getMonth() + 1;
		m = m < 10 ? "0" + m : m;
		var d = date.getDate();
		d = d < 10 ? "0" + d : d;
		var h = date.getHours();
		h = h < 10 ? "0" + h : h;
		var minute = date.getMinutes();
		minute = minute < 10 ? "0" + minute : minute;
		var second = date.getSeconds();
		second = second < 10 ? "0" + second : second;
		if(isDay){
			return y + "-" + m + "-" + d
		}else{
			return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second;
		}
	},
	//获取code
	getCode(){
		return new Promise((jre, rea) => {
			uni.login({
				provider: 'weixin',
				success: async (msg) => {
					jre(msg.code)
					// this.$u.route('/pages/store/statePayment')
				},
				fail: (err) => {
					console.log('获取code失败:', err)
					this.$u.toast('获取code失败:', err)
					rea(err)
				}
			})
		})
	},
	//去登录
	async goLogin(){
		var isCode = await this.getCode()
		uni.showModal({
			title:'提示',
			content:'您还未登录,请先登录后再操作',
			confirmColor:'#007664',
			success:(res) => {
				if(res.confirm)
				uni.getUserProfile({
					desc: '获取头像昵称', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success:async (e) => {
						let {iv,encryptedData} = e;
						uni.showLoading({
							title:'登录中...'
						})
						let {code,data} = await _this.$u.api.wxLogin({code:isCode,iv,encryptedData,fid: this.fid || ''});
						uni.hideLoading()
						if(code){
							_this.$u.toast('登录成功!');
							_this.$u.vuex('token',data.user_token)
							store.dispatch('getUserInfo');
							
							
							// this.$u.vuex('userInfo',data.user)
							// if(data.user.phone){
								// this.xin.goUrl('/pages/index/index',true)
							// }
						}
						
					}
				})
			},
			fail: () => {
				
			}
		})
	},
	goUrl(url, type) {
		let urltype = type ? 'redirectTo' : 'navigateTo';
		// if(!store.state.token&&isLog){
		// 	this.goLogin()
		// 	return;
		// }
		if (url) {
			uni[urltype]({
				url: `${url}`,
				success: () => {
					store.navFlag = false
				},
				fail: (err) => {
					console.log(err);
				}
			})
		} else {
			uni.showToast({
				title: "链接无效",
				icon: 'none'
			})
		}
	},
	backPage: (delta=1) => {
		// 返回页面
		let pages = getCurrentPages()
		if (pages.length === 1) {
			utlis.goUrl('/pages/index/index',true)
		} else {
			uni.navigateBack({
				//uni.navigateTo跳转的返回，默认1为返回上一级
				delta
			});
		}
	},
	previewImage: (imgList, index) => {
		if (!imgList) {
			this.Toast('查看图片错误')
			return false
		}
		index || 0
		// 预览图片
		uni.previewImage({
			current: index || 0,
			urls: imgList,
			indicator: 'number',
			fail: (err) => {
				console.log(err);
			},
			longPressActions: {
				success: function(data) {},
				fail: function(err) {
					console.log(err.errMsg);
				}
			}
		});
	},
	async uplodaImg(tempFilePaths) {
		console.log(store, "tojk")
		return new Promise((reslove, riject) => {
			uni.showLoading({
				title: '上传中...'
			})
			uni.uploadFile({
				url: utlis.baseUrl + '/upload', //仅为示例，非真实的接口地址
				filePath: tempFilePaths,
				name: 'files',
				header: {
					'x-user-token': store.state.token
				},
				success: (uploadFileRes) => {
					uni.hideLoading()
					reslove(JSON.parse(uploadFileRes.data))
					_this.$u.toast('上传成功!');
				},
				fail: (err) => {
					uni.hideLoading()
					console.log(err, "err")
					_this.$u.toast('上传失败' + err);
					riject(riject)
				}

			})
		})
	},

	
}
export default utlis
